from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from goods.models import SPUSpecification, SPU
from meiduo_admin.utils.paginator import PageNum
from meiduo_admin.serializers.specs import SpecsSerializer, SPUSerializer


"""
{
        "counts": "SPU商品规格总数量",
        "lists": [
            {
                "id": "规格id",
                "name": "规格名称",
                "spu": "SPU商品名称",
                "spu_id": "SPU商品id"
            },
            ...
          ],
          "page": "页码",
          "pages": "总页数",
          "pagesize": "页容量"
      }
"""


class SpecsView(ModelViewSet):
    permission_classes = [IsAdminUser]
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecsSerializer
    pagination_class = PageNum

    def simple(self, request):
        spus = SPU.objects.all()
        ser = SPUSerializer(instance=spus, many=True)

        return Response(ser.data)

